<?php
if ($do === 'insert_promoter') {
	mysql_query("INSERT INTO `promoter` SET `contact`='".$_POST['contact']."',`company`='".$_POST['company']."',`phone`='".$_POST['phone']."',`nickname`='".$_POST['nickname']."',`email`='".$_POST['email']."',`website`='".$_POST['website']."',`aim`='".$_POST['aim']."',`notes`='".$_POST['notes']."' ");
} elseif ($do === 'delete_promoter') {
	
	//let's find out who it was we just tried to delete
	$promoter_select = "SELECT * FROM `promoter` WHERE `id`='".$_GET['id']."' LIMIT 1";
	$promoter_result = mysql_fetch_array(mysql_query($promoter_select));
	
	//if the delete is a success, we'll say the following
	$success_message = $promoter_result[contact] . ' (' . $promoter_result[nickname] . ') from ' . $promoter_result[company] . '<b> has successfully been deleted from the database.</b>';
	
	//if the delete is a failure, we'll say the following
	$failure_message = 'Whoops!  Looks like an error occurred in your attempt to delete the promoter!';
	
	//now we'll do the actual deleting
	mysql_query("DELETE FROM `promoter` WHERE `id`='".$_GET['id']."' LIMIT 1"); // or die($failure_message);
	
	if ($promoter_result = mysql_fetch_array(mysql_query($promoter_select))) {
		echo($failure_message);
	} else {
		echo($success_message);
	}
	
}
if ($mode === 'promoSrch') $promoSrch = $_REQUEST[promoSrchBox];
?>
<div name="promoSrch" style="align:center; display: block; text-align: center;">
<form name="promoSrch" action="<?=$_SERVER[PHP_SELF]?>" method="post" />
<input type="text" style="width: 170px;" name="promoSrchBox" /><input type="submit" value="search" />
<input type="hidden" name="mode" value="promoSrch" /><?php 
if ($mode === 'promoSrch') echo(' Searched for "'.$promoSrch.'".');
?>
<hr /></form>
</div>

<?php 
if ($mode === 'promoEdit') {
   if ($do === 'editPromo') {
      $promoUpdateSql = "
      UPDATE `promoter` 
      SET 
      `company`='".mysql_real_escape_string($_REQUEST[company])."',
      `contact`='".mysql_real_escape_string($_REQUEST[contact])."',
      `nickname`='".mysql_real_escape_string($_REQUEST[nickname])."',
      `phone`='".mysql_real_escape_string($_REQUEST[phone])."',
      `email`='".mysql_real_escape_string($_REQUEST[email])."',
      `aim`='".mysql_real_escape_string($_REQUEST[aim])."',
      `website`='".mysql_real_escape_string($_REQUEST[website])."',
      `notes`='".mysql_real_escape_string($_REQUEST[notes])."'
      WHERE 
      (`id`='".$_REQUEST[promoId]."')
      ";
      if (mysql_query($promoUpdateSql)) {
         echo('<span class="alertMessage">Changes successfully saved.</span><br />');
      } else {
         echo('<span class="alertMessage">There was a problem.  Your changes have not been saved</span><br />');
         mysql_error();
      }
   }

   $promoSql = " SELECT * FROM `promoter` WHERE (`id`='".$_REQUEST[promoId]."') LIMIT 1 ";
   $promoQuery = mysql_query($promoSql);
   $promo = mysql_fetch_array($promoQuery);
?>
<div id="promoEdit">
<form name="promoEdit" action="<?=$_SERVER[PHP_SELF]?>" method="post">
Edit Promoter:
<br />
<table>
<tr>
<td class="inputTitleLeft">company:</td>
<td><input type="text" name="company" value="<?=$promo[company]?>" size="16" /> <span class="helpInfo"> (required)</span></td>
</tr>
<tr>
<td class="inputTitleLeft">contact name:</td>
<td><input type="text" name="contact" value="<?=$promo[contact]?>" size="18" /></td>
</tr>
<tr><td class="inputTitleLeft">aka:</td>
<td><input type="text" name="nickname" value="<?=$promo[nickname]?>" size="8"/><span class="helpInfo"> (required)</span></td></tr>
<tr>
<td class="inputTitleLeft">phone:</td><td><input type="text" name="phone" value="<?=$promo[phone]?>" size="12" /></td></tr>
<tr>
<td class="inputTitleLeft">email:</td><td><input type="text" name="email" value="<?=$promo[email]?>" size="12" /></td></tr>
<tr>
<td class="inputTitleLeft">aim:</td><td><input type="text" name="aim" value="<?=$promo[aim]?>" size="16" /></td></tr>
<tr>
<td class="inputTitleLeft">website:</td><td><input type="text" name="website" value="<?=$promo[website]?>" size="18" /><span class="helpInfo"> (omit the "http://")</span></td></tr>
<tr><td class="inputTitleLeft">notes:</td>
<td><textarea style="width: 300px; height: 140px;" name="notes"><?=$promo[notes]?></textarea></td></tr>
<tr><td></td><td>
<input type="hidden" name="promoId" value="<?=$_REQUEST[promoId]?>" />
<input type="hidden" name="mode" value="promoEdit" />
<input type="hidden" name="do" value="editPromo" />
<input name="submit_form" value="Save Changes" type="submit" />
<input type="reset" /></td></tr>
</table>
</form>
</div>

<?php 
}
?>



<div class="hidden" id="promoAdd">
<form name="promoter_add" action="<?=$_SERVER[PHP_SELF]?>" method="post">
Add Promoter:
<br />
<table>
<tr>
<td class="inputTitleLeft">company:</td>
<td><input type="text" name="company" size="16" /> <span class="helpInfo"> (required)</span></td>
</tr>
<tr>
<td class="inputTitleLeft">contact name:</td>
<td><input type="text" name="contact" size="18" /></td>
</tr>
<tr><td class="inputTitleLeft">aka:</td>
<td><input type="text" name="nickname" size="8"/><span class="helpInfo"> (required)</span></td></tr>
<tr>
<td class="inputTitleLeft">phone:</td><td><input type="text" name="phone" size="12" /></td></tr>
<tr>
<td class="inputTitleLeft">email:</td><td><input type="text" name="email" size="12" /></td></tr>
<tr>
<td class="inputTitleLeft">aim:</td><td><input type="text" name="aim" size="16" /></td></tr>
<tr>
<td class="inputTitleLeft">website:</td><td><input type="text" name="website" value="www." size="18" /><span class="helpInfo"> (omit the "http://")</span></td></tr>
<tr><td class="inputTitleLeft">notes:</td>
<td><textarea style="width: 300px; height: 140px;" name="notes"></textarea></td></tr>
<tr><td></td><td><input type="hidden" name="do" value="insert_promoter" />
<input type="hidden" name="mode" value="promoter" />
<input name="submit_form" value="Add Promoter" type="submit" />
<input type="reset" /></td></tr>
</table>
</form>
</div>


<input type="button" value="add new promoter &gt;&gt;" onClick="document.getElementById('promoAdd').className = 'normal'; " /><br />


<table><?php
$promoSql = " SELECT * FROM `promoter` ";
if ($mode === 'promoSrch') $promoSql .= " WHERE ( `company` LIKE '%".$promoSrch."%' ) OR  ( `contact` LIKE '%".$promoSrch."%' ) OR ( `nickname` LIKE '%".$promoSrch."%' ) ";
$promoSql .= " ORDER BY `company` ASC ";
$promoQuery = mysql_query($promoSql);
while($promoter = mysql_fetch_array($promoQuery)){
	?><tr>
	<td><a href="http://<?=$promoter[website]?>"><?=$promoter[company]?></a> (<?=$promoter[nickname]?>)</td>
	<td style="padding-left:3px"><?=$promoter[email]?></td><td style="padding-left:3px"><?=$promoter[phone]?></td>
	<td style="padding-left:3px"><? if ($promoter[aim]) { ?><a href="aim:goim?screenname=<?=$promoter[aim]?>"><img style="border: 0px;" height="16" width="16" title="Send this person a little aim" alt="aim" src="./themes/<?=$theme?>/img/im-aim-16.png" /></a><?=$promoter[aim]?><?php } ?></td>
	<td><a href="?mode=promoter&do=delete_promoter&id=<?=$promoter[id]?>" title="delete this promoter from the database" onclick="return confirm('Do you really want to delete this promoter from the database?')"><img style="border: 0px;" height="16" width="16" title="delete this promoter" alt="trash, delete" src="./themes/<?=$theme?>/img/trash.png" /></a>
	<a href="?mode=promoEdit&promoId=<?=$promoter[id]?>" title="edit this promoter's information"><img style="border: 0px;" height="16" width="16" title="edit this promoter" alt="edit" src="./themes/<?=$theme?>/img/edit.png" /></a></td>
	</tr>
<?php
}
?>
</table>
<?php 
if ($mode === 'promoEdit') {
   echo('editing promoter with id#'.$_REQUEST[promoId].'.');
}
?>
